package com.ld.admin.controller.system.codegen;

import javax.validation.Valid;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.codegen.CodegenDbsetModel;
import com.ld.admin.service.codegen.CodegenService;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;
import com.ld.shieldsb.db.model.DBSetInfo;
import com.ld.shieldsb.db.service.DBTabelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "代码生成器-数据库设置")
@ConditionalOnProperty(prefix = "modules.sc.system.codegen", name = "dbset.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "CodegenDbsetController")
@RequestMapping("${shieldsb.adminPath}/system/codegen/dbset")
public class CodegenDbsetController extends AdminBasicController<CodegenDbsetModel> {

    @Autowired
    public CodegenService codegenService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/system/codegen/dbset/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/system/codegen/dbset/dbset_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = basicService.getSearchQueryModel(request);
        basicService.getPageBean(request, getModelClass(), queryModel);
        return "a/system/codegen/dbset/dbset_list";
    }

    @ApiOperation(value = "代码生成器-数据库设置展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        basicService.getOne(request, getModelClass(), id);
        return "a/system/codegen/dbset/dbset_show";
    }

    @ApiOperation(value = "跳转到添加/修改代码生成器-数据库设置", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        basicService.getOne(request, getModelClass(), id);
        return "a/system/codegen/dbset/dbset_update";
    }

    @ApiOperation(value = "添加/修改代码生成器-数据库设置", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid CodegenDbsetModel model) {
        Result result = basicService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除代码生成器-数据库设置", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = basicService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "连接测试-数据库设置", notes = "")
    @RequestMapping(path = "/testconn", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object testconn() {
        String dbConnIdStr = getString("id");
        DBSetInfo dbSetInfo = null;
        Result result = new Result();
        if (StringUtils.isNotBlank(dbConnIdStr)) {
            long dbConnId = getLong("id");
            dbSetInfo = codegenService.getDbSetInfo(dbConnId);
        } else {
            String dbUrl = StringEscapeUtils.unescapeHtml(getString("dbUrl"));
            String dbUserName = StringEscapeUtils.unescapeHtml(getString("dbUserName"));
            String dbPassword = StringEscapeUtils.unescapeHtml(getString("dbPassword"));
            dbSetInfo = codegenService.getDbSetInfo(dbUrl, dbUserName, dbPassword);
        }
        if (dbSetInfo != null) {
            DBTabelService.getInstance(dbSetInfo);
            result = DBTabelService.testCon(dbSetInfo.getDbUrl(), dbSetInfo.getDbUserName(), dbSetInfo.getDbPassword());
        }
        String msg = "连接失败！";
        if (result != null) {
            if (result.getSuccess()) {
                return Web.Response.success("连接成功！");
            } else {
                msg += result.getMessage();
            }
        }
        return Web.Response.error(msg);
    }

}